package com.qifan.alb2b.dao;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import com.cqcis.base.orm.jdbc.JdbcUtil;
import com.qifan.alb2b.domain.GuideInfoDomain;
import com.qifan.alb2b.domain.HotelInfoDomain;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.Page;

/**
 * 接送CURD
 * @author hc24
 *
 */
@Repository
public class GuideInfoDao extends BaseDao {
	private final static Log logger=LogFactory.getLog(GuideInfoDao.class);
	
	/**
	 * 根据条件分页查询导游信息
	 * @param dgm 分页参数
	 * @param gid 导游查询参数
	 * @return Page对象（分页信息&分页数据）
	 */
	public Page query(DataGridModel dgm,GuideInfoDomain gid){
		logger.debug("导游查询");
		/* 构建sql */
		StringBuilder sql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		sql.append("select * from guideinfo where 1=1");
		if(gid.getGuideLan()!=-1){
			sql.append(" and guideLan=?");
			params.add(gid.getGuideLan());
		}
		if(gid.getCityId()!=null&&!"".equals(gid.getCityId())){
			sql.append(" and cityId=?");
			params.add(gid.getCityId());
		}
		
		//判断是否排序
		if(dgm.getSort()!=null&&!"".equals(dgm.getSort()))
			sql.append(" order by ").append(dgm.getSort()).append(" ").append(dgm.getOrder());
		return super.queryObjByPage(sql.toString(), dgm.getRows(), dgm.getPage(), params, GuideInfoDomain.class);
	}
	
	public List<GuideInfoDomain> queryGuidesByCitys(String[] cityIds){
		StringBuilder sql=new StringBuilder();
		sql.append("select * from guideInfo where cityId in(");
		for(int i=0;i<cityIds.length-1;i++){
			sql.append("?,");
		}
		sql.append("?)");
		return JdbcUtil.queryForList(this, GuideInfoDomain.class, sql.toString(), cityIds);
	}
	
	public List<GuideInfoDomain> queryGuidesByIds(String[] ids){
		StringBuilder sql=new StringBuilder();
		sql.append("select * from guideInfo where guideId in(");
		for(int i=0;i<ids.length-1;i++){
			sql.append("?,");
		}
		sql.append("?)");
		return JdbcUtil.queryForList(this, GuideInfoDomain.class, sql.toString(), ids);
	}
	
	public GuideInfoDomain getGuideById(String gid){
		String sql="select * from guideInfo where guideId=?";
		List<GuideInfoDomain> list=JdbcUtil.queryForList(this, GuideInfoDomain.class, sql, gid);
		if(list.size()>0)
			return list.get(0);
		return null;
	}
	
	
	public void add(GuideInfoDomain guideInfoDomain){
		super.addObj(guideInfoDomain);
	}
	
	public void update(GuideInfoDomain guideInfoDomain){
		super.updateObj(guideInfoDomain, "guideId");
	}
	
	public void delete(String[] ids){
		StringBuilder delSql=new StringBuilder();
		delSql.append("delete from guideInfo where guideId in(");
		for(int i=0;i<ids.length-1;i++){
			delSql.append("?,");
		}
		delSql.append("?)");
		logger.debug("删除sql："+delSql+"["+ids+"]");
		this.getJdbcTemplate().update(delSql.toString(), ids);
	}
}
